import { Entity, Column, Index, OneToMany } from 'typeorm';
import { BaseEntity } from '../common/base/base.entity';
import { Product } from './product.entity';

/**
 * 商品分类实体
 */
@Entity('categories')
@Index('idx_parent', ['parentId'])
@Index('idx_sort', ['sort'])
export class Category extends BaseEntity {
  @Column({
    type: 'varchar',
    length: 100,
    comment: '分类名称',
  })
  name: string;

  @Column({
    type: 'varchar',
    length: 100,
    nullable: true,
    unique: true,
    comment: '分类标识（英文）',
  })
  slug?: string;

  @Column({
    type: 'text',
    nullable: true,
    comment: '分类描述',
  })
  description?: string;

  @Column({
    type: 'varchar',
    length: 500,
    nullable: true,
    comment: '分类图标',
  })
  icon?: string;

  @Column({
    type: 'bigint',
    nullable: true,
    comment: '父分类ID',
  })
  parentId?: string;

  @Column({
    type: 'int',
    default: 0,
    comment: '排序权重',
  })
  sort: number;

  @Column({
    type: 'boolean',
    default: true,
    comment: '是否显示',
  })
  isVisible: boolean;

  // 关联：一个分类下有多个商品
  @OneToMany(() => Product, (product) => product.category)
  products?: Product[];
}
